package day2Test4;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;


/*
 * 1,有一个水果销售摊位,销售3种水果,重量和单价各不相同,实现多次的销售业务 
 * 2,销售时如果为顾客为女性销售金额打8折 
 * 3,显示当前各种水果的库存数
 * 4,查询全部销售记录信息 
 * 5,加入其它水果品种 
 * 6,添加进货单 
 * 7,查询指定日期间隔的销售纯利润 
 * 8,每日17: :00之后为5折特价
 * 9,按销售数量排序显示前三名 10,示当前各种水果的库存数,成本单价,单价,累计销售数量/金额,累计进货数量/金额
 */
public class Funcation {

	static Funcation funcation = new Funcation();

	static Scanner scanner = new Scanner(System.in);

	static FruitStand fruitStand = new FruitStand(); 

	// 写文件
	public static void WriteTxt(FruitStand fruitStand) throws Exception {
		FileOutputStream fos = new FileOutputStream("fruitStands.txt");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(fruitStand);
		oos.flush();
		oos.close();
	}

	// 读文件
	public static FruitStand ReadTxt() throws Exception {
		FileInputStream fis = new FileInputStream("fruitStands.txt");

		ObjectInputStream oin = new ObjectInputStream(fis);

		fruitStand = (FruitStand) oin.readObject();

		oin.close();
		fis.close();
		return fruitStand;

	}

	// 初始化信息
	public static void initialize() throws Exception {
		WriteTxt(fruitStand);
	}

	// 保留double 类型的两位小数
	public static double Change(double shu) {
		BigDecimal df = new BigDecimal(shu);
		double f1 = df.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

		return f1;
	}

	// 添加水果信息
	public static void addFruits() throws Exception {
		ArrayList<Fruits> fruits = new ArrayList<>();
		fruitStand = ReadTxt();
		if (fruitStand.getFruits() != null) {
			fruits = fruitStand.getFruits();
		}

		Fruits fruit = new Fruits();

		System.out.println("请输入名称");
		String name = scanner.nextLine();
		fruit.setName(name);
		System.out.println("请输入重量");
		double weight = Double.parseDouble(scanner.nextLine());
		fruit.setWeight(weight);
		System.out.println("请输入单价");
		double selling_price = Double.parseDouble(scanner.nextLine());
		fruit.setSelling_price(selling_price);
		System.out.println("请输入成本价");
		double cost_price = Double.parseDouble(scanner.nextLine());
		fruit.setCost_price(cost_price);
		System.out.println("请输入库存");
		double repertory = Double.parseDouble(scanner.nextLine());
		fruit.setRepertory(repertory);

		fruit.setAmount_purches(repertory); // 累计进货量

		double Amount_purches_cost = Change(cost_price * repertory); // 累计进货金额

		fruit.setAmount_purches_cost(Amount_purches_cost);

		fruits.add(fruit);

		fruitStand.setFruits(fruits);
		WriteTxt(fruitStand);

	}

	// 销售水果
	public static void sellingFruits() throws Exception {
		ArrayList<Fruits> fruits = new ArrayList<>();
		fruitStand = ReadTxt();
		fruits = fruitStand.getFruits();
		int hour = 0;
		double[] profits = new double[24];
		double profit = 0;
		ArrayList<String> records = new ArrayList<>();
		if (fruitStand.getRecords() != null) {
			records = fruitStand.getRecords();
		}
		while (true) {
			double total = 0.0;
			System.out.println("请输入时间（整数小时）");
			hour = Integer.parseInt(scanner.nextLine());
			System.out.println("请输入水果名称");
			String name = scanner.nextLine();
			boolean flagname = false; // 姓名判断标志
			boolean flagamount = false;// 数量判断标志
			double cost = 0; // 水果花费
			for (Fruits fruit : fruits) {
				if (name.equals(fruit.getName())) {
					flagname = true;
					System.out.println("请输入重量");
					double weight = Double.parseDouble(scanner.nextLine());
					double repertory = fruit.getRepertory();
					double sales = fruit.getSales();
					if (weight < repertory) {
						flagamount = true; // 判断数量

						double cost_price = Change(fruit.getCost_price());
						double selling_price = Change(fruit.getSelling_price());

						System.out.println("请输入是否为女性，是请输入‘1’，否请输入‘0’");
						int judgesex = Integer.parseInt(scanner.nextLine());
						if (hour >= 17) {
							if (judgesex == 0) {
								cost = weight * selling_price * 0.5; // 计算花费
							} else {

								cost = weight * selling_price * 0.8 * 0.5;
							}
							total = Change(total + cost);
							System.out.println("消费为:" + total);

						} else {
							if (judgesex == 0) {
								cost = weight * fruit.getCost_price(); // 计算花费
							} else {
								cost = weight * fruit.getCost_price() * 0.8;
							}
							total = Change(total + cost);
							System.out.println("消费为:" + total);

						}
						profit = Change((selling_price - cost_price) * weight); // 计算净利润

						records.add(hour + "时" + "售出" + name + "," + weight + "斤，金额为：" + total); // 增加销售记录

						fruit.setRepertory(repertory - weight); // 库存减少

						fruit.setSales(weight + sales); // 销量增加
					} else {
						System.out.println("库存不足");
					}

					break;
				}
			}
			profits[hour] += profit;
			System.out.println("是否要继续购买，是请输入'1'否请输入'0'");
			int flag = Integer.parseInt(scanner.nextLine());
			if (flag == 0) {
				break;
			}
		}

		fruitStand.setProfit(profits);
		fruitStand.setRecords(records);
		fruitStand.setFruits(fruits);
		WriteTxt(fruitStand);
	}

	// 显示所有消费信息
	public static void showAllRecords() throws Exception {
		ArrayList<String> records = new ArrayList<>();

		fruitStand = ReadTxt();
		records = fruitStand.getRecords();
		int i = 1;
		if (records != null) {
			for (String record : records) {
				System.out.println(i + " " + record);
			}
		} else {
			System.out.println("没有记录");
		}
	}

	// 显示当前各种水果的库存数
	public static void showFruitsRepertory() throws Exception {
		fruitStand = ReadTxt();
		ArrayList<Fruits> fruits = fruitStand.getFruits();
		int i = 1;
		if (fruits != null) {
			for (Fruits fruit : fruits) {
				String name = fruit.getName();
				double repertory = fruit.getRepertory();
				System.out.println(i + " " + name + "的库存为：" + repertory + "斤");
				i++;
			}
		} else {
			System.out.println("没有记录");
		}
	}

	// 添加进货单
	public static void addPurchesList() throws Exception {
		fruitStand = ReadTxt();

		PurchesList purchesList = new PurchesList();
		System.out.println("请输入水果名称");
		String name = scanner.nextLine();
		System.out.println("请输入重量");
		double weight = Double.parseDouble(scanner.nextLine());
		System.out.println("请输入成本价");
		double cost_price = Double.parseDouble(scanner.nextLine());
		System.out.println("请输入销售价");
		double selling_price = Double.parseDouble(scanner.nextLine());
		ArrayList<Fruits> fruits = new ArrayList<>();
		if (fruitStand.getFruits() != null) {
			fruits = fruitStand.getFruits();
		}
		boolean flag = false;
		double total = 0;
		if (fruits != null) {
			for (Fruits fruit : fruits) {
				if (name.equals(fruits)) {
					double repertory = fruit.getRepertory();
					fruit.setRepertory(repertory + weight);
					fruit.setCost_price(cost_price);
					fruit.setSelling_price(selling_price);
					total = weight * cost_price;
					double accruing_amount = fruit.getAccruing_amounts();
					fruit.setAccruing_amounts(accruing_amount + weight);
					double amount_purches_cost = fruit.getAmount_purches_cost() + total;
					fruit.setAmount_purches_cost(amount_purches_cost);
					flag = true;
					break;
				}
			}
		}
		if (fruits == null || flag == false) {
			Fruits fruit = new Fruits();
			total = weight * cost_price;

			fruit.setName(name);
			fruit.setWeight(weight);
			fruit.setRepertory(weight);
			fruit.setCost_price(cost_price);
			fruit.setSelling_price(selling_price);
			fruit.setAccruing_amounts(weight);
			fruit.setAmount_purches_cost(total);
			fruits.add(fruit);
		}
		fruitStand.setFruits(fruits);
		WriteTxt(fruitStand);
	}

	// 按照销量显示前三名
	public static void showTopThree() throws Exception {
		fruitStand = ReadTxt();
		ArrayList<Fruits> fruits = new ArrayList<Fruits>();
		if (fruitStand.getFruits() != null) {
			fruits = fruitStand.getFruits();
		}
		
	
		Collections.sort(fruits);
		
		
//		TreeSet<Fruits> fruits2 = new TreeSet<>();
//		if (fruits != null) {
//			for (Fruits fruit : fruits) {
//				fruits2.add(fruit);
//			}
//	}
			int i = 3;
			for (Fruits fruit : fruits) {
				if (i == 0) {
					break;
				}
				System.out.println(fruit.getName() + "销量为" + fruit.getSales() + "斤，库存数 ：" + fruit.getRepertory()
						+ "成本价为： " + fruit.getCost_price() + "单价为： " + fruit.getSelling_price() + "总进货量为："
						+ fruit.getAmount_purches() + "斤，总进货金额" + fruit.getAmount_purches_cost());
				i--;
			}
		}
	

	// 7,查询指定日期间隔的销售纯利润
	public static void appointProfit() throws Exception {
		fruitStand = ReadTxt();
		System.out.println("请输入开始时间段");
		int time1 = Integer.parseInt(scanner.nextLine());
		System.out.println("请输入结束时间段");
		int time2 = Integer.parseInt(scanner.nextLine());
		double[] profit = fruitStand.getProfit();
		double sectionProfit = 0;
		for (int i = time1; i < time2; i++) {
			sectionProfit += profit[i];
		}
		System.out.println(time1 + "时到" + time2 + "时，之间的纯利润为：" + sectionProfit);

	}

	public static void showMenu() {
		System.out.println("---------0.初始化信息（首次使用必须初始化信息）			  -----------");
		System.out.println("---------1.添加水果信息			  -----------");
		System.out.println("---------2. 销售水果  			  -----------");
		System.out.println("---------3.显示所有消费信息       -----------");
		System.out.println("---------4.显示当前各种水果的库存数 -----------");
		System.out.println("---------6.输出销量前三的            -----------");
		System.out.println("---------7.查询某时间段的纯利润		      -----------");
		System.out.println("---------#.退出          -----------");
	}

	public static void chooseMenu() throws Exception {

		while (true) {
			showMenu();
			System.out.println("请输入标号进行功能选择");
			int choose = Integer.parseInt(scanner.nextLine());
			switch (choose) {
			case 0: {
				initialize();
				break;
			}
			case 1: {
				addFruits();
				break;
			}
			case 2: {
				sellingFruits();
				break;
			}
			case 3: {
				showAllRecords();
				break;
			}
			case 4: {
				showFruitsRepertory();
				break;
			}
			case 5: {
				addPurchesList();
				break;
			}
			case 6: {
				showTopThree();
				break;
			}
			case 7: {
				appointProfit();
				break;
			}
			default:
				throw new IllegalArgumentException("输入错误，请重新输入 ");
			}
			System.out.println("退出请输入0,否则请输入任意");
			String flag = scanner.nextLine();
			if ("#".equals(flag)) {
				break;
			}
		}
	}
}
